<!doctype html>
<html>
<head>
	<script src="http://lib.baomitu.com/lodash.js/4.17.21/lodash.js"></script>
	<script src="http://lib.baomitu.com/jquery/3.6.0/jquery.min.js"></script>
	<script src="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.js" type="text/javascript"></script>
	<script src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.js"></script>
	
	<link href="https://swisnl.github.io/jQuery-contextMenu/dist/jquery.contextMenu.css" rel="stylesheet" type="text/css" />
	
	<style>
		html,body {
			width: 100%;
			height: 100%;
			margin: 0;
		}
	</style>
</head>
<body>

	

	<textarea class="txt" style="width: 40%; height: 40%;"></textarea>
	<textarea class="txt" style="width: 40%; height: 40%;"></textarea>
	<textarea class="txt" style="width: 40%; height: 40%;"></textarea>
	<textarea class="txt" style="width: 40%; height: 40%;"></textarea>

	<div style="display: none" class="btn"></div>

</body>

</html>
<script>
var cookie = 'sid=SESS16afd42d-fc88-4c15-b6c8-6d07a8bcfa85;';

// 消息提示封装
(function() {
	var msgTools = {
		toast: function(msg, duration) {
		  duration = !duration ? 3000 : duration;
		  var m = document.createElement('div');
		  m.innerHTML = msg;
		  m.style.cssText="max-width:60%;min-width: 150px;padding:0 14px;height: 40px;color: rgb(255, 255, 255);line-height: 40px;text-align: center;border-radius: 4px;position: fixed;top: 50%;left: 50%;transform: translate(-50%, -50%);z-index: 999999;background: rgba(0, 0, 0,.7);font-size: 16px;";
		  document.body.appendChild(m);
		  setTimeout(function() {
			var d = 0.5;
			m.style.webkitTransition = '-webkit-transform ' + d + 's ease-in, opacity ' + d + 's ease-in';
			m.style.opacity = '0';
			setTimeout(function() { document.body.removeChild(m) }, d * 1000);
		  }, duration);
		}
	};
	
	window.msgTools = msgTools;
})();

// 封装复制方法
(function() {
	var __clipText = null;
	var clipboard = new ClipboardJS('.btn', {
		text: function(trigger) {
			return __clipText;
		}
	});
	clipboard.on('success', function() {
		msgTools.toast('复制成功');
	});
	clipboard.on('error', function() {
		msgTools.toast('复制失败', arguments);
	});
	
	function copy(txt) {
		__clipText =  txt;
		$('.btn').trigger('click');
	}
	window.copy = copy;
})();


$('.txt').change(function() {
	var index = $(this).index();
	var value = this.value;
	try	{
		value = JSON.parse(value);
	} catch(msg) {}
	window['jsonValue' + index] = value;
});

$.contextMenu({
	selector: '.txt', 
	callback: function(key, options) {
		var m = "clicked: " + key;
		if (options.commands[key].command) {
			options.commands[key].command.apply(options.$trigger[0]);
		}
		// window.console && console.log(m) || alert(m); 
	},
	items: {
		"array.join(',')": {
			name: "array.join(',')", 
			icon2: "edit", 
			command: function() {
				var text = this.value.trim();
				copy(text.split('\n').join(','));
			}
		},
		"array.map(\"'x'\").join(',')": {
			name: "array.map(\"'x'\").join(',')", 
			icon2: "edit", 
			command: function() {
				var text = this.value.trim();
				copy(text.split('\n').map(d => `'${d}'`).join(','));
			}
		},
		"distinct(array).join(',')": {
			name: "distinct(array).join(',')", 
			icon2: "edit", 
			command: function() {
				var text = this.value.trim();
				copy(_(text.split('\n')).uniq().value().join(','));
			}
		},
		"JSON.stringify": {
			name: "JSON.stringify", 
			icon2: "edit", 
			command: function() {
				var text = this.value.trim();
				copy(JSON.stringify(text));
			}
		},
		"parseJsonThenJSON.stringify": {
			name: "parseJsonThenJSON.stringify", 
			icon2: "edit", 
			command: function() {
				var text = this.value.trim();
				var json = text;
				try {
					json = JSON.parse(text);
				} catch(msg) {}
				copy(JSON.stringify(json));
			}
		},
	},
});




</script>